﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Intorise.Common;
using Intorise.API;

namespace TestWindowsFormsApplication
{
    public partial class Form1 : Form
    {
        Bitmap bitmap = null;
        Bitmap bitmap0 = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    bitmap = (Bitmap)Bitmap.FromFile(openFileDialog1.FileName);
                    if (this.pictureBox1.Image != null)
                    {
                        this.pictureBox1.Image.Dispose();
                    }
                    this.pictureBox1.Image = bitmap;
                    this.textBox.Text = openFileDialog1.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误", "打开图像出错！");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                try
                {
                    bitmap0 = (Bitmap)Bitmap.FromFile(openFileDialog1.FileName);
                    if (this.pictureBox2.Image != null)
                    {
                        this.pictureBox2.Image.Dispose();
                    }
                    this.pictureBox2.Image = bitmap0;
                    this.textBox1.Text = openFileDialog1.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误", "打开图像出错！");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap map = (Bitmap)bitmap.Clone();
            ImageTools.Convert2GrayFast(map);
            //GrayByPixels(map);
            if (this.pictureBox1.Image != null && this.pictureBox1.Image != bitmap)
            {
                this.pictureBox1.Image.Dispose();
            }
            this.pictureBox1.Image = map;
        }

        /// <summary>
        /// 根据RGB，计算灰度值
        /// </summary>
        /// <param name="posClr">Color值</param>
        /// <returns>灰度值，整型</returns>
        private static int GetGrayNumColor(System.Drawing.Color posClr)
        {
            return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (bitmap != null && bitmap0 != null)
            {
                DateTime start = DateTime.Now;
                //Point point = ImageTools.SearchImage(bitmap, bitmap0, new Rectangle(500, 300, 500, 300), 0.8);
                ImageTools.PixelColorRedundance = 30;//这个是设置每个点的颜色容差，不设置默认就是30，我测试30就可以
                Point point = ImageTools.SearchImage(bitmap, bitmap0, 0.8);
                //0.8是最终的相似度，
                //1要完全相同，0.8允许有20%的像素点不一样。
                //优化后的代码，现在的分辨率，搜索全尺寸5秒左右，如果限定范围一般1-2秒可以找到
                //查找图片的方法SearchImage和从图像里获取一个点的颜色的方法GetPixel都搬到ImageTools这个类里了。
                //刚才修改了发送按键的代码，我测试可以正常运行。上次的可能哪里改动导致有问题。
                TimeSpan span = DateTime.Now - start;
                if (point.X > -1 && point.Y > -1)
                {
                    //MessageBox.Show("Searched, used time:" + span.TotalMilliseconds + " ms.");
                    Graphics g = Graphics.FromImage(bitmap);
                    Pen pen = (Pen)Pens.Blue.Clone();
                    pen.Width = 2;
                    g.DrawRectangle(pen, point.X - 2, point.Y - 2, bitmap0.Width + 4, bitmap0.Height + 4);
                }
                else
                {
                    MessageBox.Show("Can not searched, used time:" + span.TotalMilliseconds + " ms.");
                }
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show("Form1_KeyDown " + e.KeyCode);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Helper.SendKeyEx(this.Handle, Keys.Q);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            MessageBox.Show("Form1_KeyUp " + e.KeyCode);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.pictureBox1.Image == null) { return; }
            //Point point = this.pictureBox1.PointToClient(new Point(e.X, e.Y));
            int color = ImageTools.GetPixel(bitmap, e.X, e.Y);
            this.textBox2.Text = "X:"+e.X+", Y:"+e.Y+", color:"+color;
        }
    }
}
